<?php
class IndexController extends Zendvn_Controller_Action
{
	public function init()
	{
	
		//Thiet dat duong dan Path den thu muc Template.Va lua chon Template duoc load cho Module
		$template_path=TEMPLATE_PATH."/public/system"; //Lay file layout la file index.phtml trong /template/public/system
		$this->loadTemplate($template_path);//Load template 'system' thay vi template 'admin'
	
	
	}
	public function indexAction()
	{
		//$hai ="Nguyen Nam Hai";
		//$this->view->hai = $hai;
		
		// CHUA XU LY DUOC PHAN TRANG THEO TUNG CHU DE BAI POST(THEO CATAGORY)
		/*action body
		 * Get new posts and paginate
		 */
		
		$posts = new Default_Model_DbTable_Posts();
		
		$result = $posts->getPosts();//Tham bien 'result' la mot array lay ve tong so post 
		$page = $this->_getParam('page',1); //Lay 'page' cua 1 page tu link URL
		
		// Object of Zend_Paginator
		$paginator = Zend_Paginator::factory($result);//Khoi tao mot doi tuong $paginator bang tong so rows
		$paginator->setItemCountPerPage(5); //Tinh so trang cua $paginator thong qua phuong thuc setItemCountPerPage voi Set Item(posts of page)
		$paginator->setCurrentPageNumber($page);//Hien thi so trang hien tai cua $paginator co id ='page')thong qua phuong thuc setCurrentPageNumber()
		
		$this->view->paginator = $paginator;
	
	}
	
	public function loginAction()
	{
		/*CHUA HIEU RO VE MOT SO METHOD VA PROPERTY
		 * 
		 * Creating $loginForm object of class Form_Login
		 */
			$loginForm = new Default_Form_Login();
			$this->view->loginForm = $loginForm;
		/*
		 * Trying to redirect to the page from which it came
		 */
		$redirect = $this->getRequest()->getParam('redirect', 'index/index');
		$loginForm->setAttrib('redirect', $redirect );
		/*
		 * Get the Zend_Auth instance
		 */
		$auth = Zend_Auth::getInstance();
		/*
		 * Check whether it has any identity , else check whether the login form is submitted
		 */
		if(Zend_Auth::getInstance()->hasIdentity()) {//Neu khoi tao mot doi tuong zend_Auth va da duoc chung thuc (vi du da luu trong cookie)thi chuyen den trang 'index/hello'
			$this->_redirect('/index/hello');
		} else if ($this->getRequest()->isPost()) { //Check form da duoc submitted hay chua
			if ( $loginForm->isValid($this->getRequest()->getPost()) ) { //Kiem tra Valid cua du lieu nhap vao form
				/*
				 * Get the username
				 */
				$username = $this->getRequest()->getPost('username'); //username la value cua user tren Form_login 
				/*
				 * Get password
				 */
				$pwd = $this->getRequest()->getPost('pass');//pass la value cua password tren Form_login
				/*
				 * Create object $authAdapter of class Model_AuthAdapter
				 */
				$authAdapter = new Default_Model_AuthAdapter($username, $pwd);
				/*
				 * Try to authenticate and check whether its valid
				 */
				$result = $auth->authenticate($authAdapter);
				
				if(!$result->isValid()) {
					switch ($result->getCode()) {
						case Zend_Auth_Result::FAILURE_CREDENTIAL_INVALID:
							$this->view->error = 'user credentials not found';
					}
				} else {
					/*
					 * If its valid redirect it . Now it will not work ;) . 
					 * Have not implemented the redirect to the page from where it came.
					 */
					$this->_redirect( $redirect ); //Neu ton tai $result thi _redirect($redirect)<=>redirect trang 'index/index'
				}
			}
		}
	}
	
	public function logoutAction()
	{
		/*
		 * Logout and clear session
		 */
		$auth = Zend_Auth::getInstance();
		$auth->clearIdentity();
		$this->_redirect('/');
	}
	
	public function registerAction()
	{
		/*CHUA XU LY DUOC MA HOA MD5 CHO PASSWORD KHI NHAP VAO CSDL
		 * CHUA XU LY DUOC BAT LOI KHI INSERT TRUNG USERSNAME
		 * CHUA XU LY DUOC HIEN THI THONG BAO SUCCESS VA QUAY TRO VE TRANG INDEX KHI REGISTER THANH CONG
		 * 
		 * Register for new account
		 */
		$register = new Default_Form_Registration();
		
		if(Zend_Auth::getInstance()->hasIdentity()) {//Neu nhu user dang login tuc la da duoc chung thuc thi ko duoc Register
			$this->_redirect('/index/hello');
		} else if ($this->getRequest()->isPost()) {
			if ( $register->isValid($this->getRequest()->getPost()) ) {
				$data = array(
					"Username" 	=>$this->getRequest()->getPost('username'),
					"Password" 	=>md5($this->getRequest()->getPost('password')),
					"Email" =>$this->getRequest()->getpost('email')
				);
			
				$inserts = new Default_Model_DbTable_register();
				$inserts->insert($data);
				//$this->_redirect('/');
				//echo 'Ban da registed success.Ban hay vui long Click';
				//echo '<a href="/index/index">Vao Day</a> De tro ve trang Index.<br />';
			}	
		}	
		$this->view->register = $register;
	}
	
	public function forgotpasswordAction()
	{
		/*CHUA CO KHOI LENH XU LY FORGOT PASSWORD
		 * 
		 * User submits for new password 
		 */
		$formforgot = new Default_Form_ForgotPassword();
		if(Zend_Auth::getInstance()->hasIdentity()) {
			$this->_redirect('/index/hello');
		} else if ($this->getRequest()->isPost()) {
			if ( $formforgot->isValid($this->getRequest()->getPost()) ) {
				//Khoi lenh xu ly du lieu forgotPassword o day.(Chung thuc Acc bang cach so sanh gia tri mail nhap vao tu form va mail trong CSDL)
			}	
		}	
		$this->view->form = $formforgot;
	}
}